import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../lib_baseview/lib_baseview.dart';
import '../../../../lib_baseview/utils/constant/text_styles.dart';
import '../../../../lib_baseview/widget/themes.dart';
import '../../../../resource/language/generated/l10n.dart';
import '../../interface/root_proxy_model.dart';
import 'cars_widget_model.dart';

class CarsWidget extends StatelessWidget {
  // final Channel? channel;

  const CarsWidget();

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProxyProvider<RootProxyModel, CarsWidgetModel>(
            create: (context) => CarsWidgetModel(),
            update: (context, rootProxyModel, leafModel) {
              if (leafModel == null) throw ArgumentError.notNull('leafModel');
              leafModel.roomModel = rootProxyModel;
              return leafModel;
            },
          ),
        ],
        child: Consumer<CarsWidgetModel>(builder: (context, model, child) {
          return _foldedStatusWidget(context, model);
        }));
  }

  Widget _foldedStatusWidget(BuildContext context, CarsWidgetModel model) {
    String carUrl = "";
    String avatarUrl = "";
    // if (model.roomModel.channel?.vipPark != null &&
    //     model.roomModel.channel!.vipPark.isNotEmpty) {
    //   carUrl = model.roomModel.channel!.vipPark[0].car.carMetaInfo.rightImgLink;
    //   avatarUrl = model.roomModel.channel!.vipPark[0].avatar;
    // }

    return CupertinoButton(
      minSize: 0,
      padding: EdgeInsets.zero,
      onPressed: () {
        model.openCarListPage(context);
      },
      child: SizedBox(
        width: 84,
        height: 81,
        child: Stack(
          alignment: AlignmentDirectional.topCenter,
          children: [
            Positioned(
              top: 16,
              child: Container(
                width: 72,
                height: 65,
                decoration: BoxDecoration(
                  color: RColors.white14,
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
            ),
            if (carUrl.isNotEmpty)
              Positioned(
                top: 30,
                child: CachedNetworkImage(
                  imageUrl: carUrl,
                  width: 60,
                  height: 38,
                  fit: BoxFit.contain,
                  errorWidget: (context, url, error) {
                    return Text('SvgPicture.asset');
                    // return SvgPicture.asset('assets/images/ic_default_car.svg');
                  },
                ),
              ),
            if (carUrl.isNotEmpty)
              const PositionedDirectional(
                top: 49,
                end: 12,
                child: Text('AvatarImage'),
                // child: AvatarImage(
                //   url: avatarUrl,
                //   width: 20,
                //   height: 20,
                //   borderRadius: 10,
                // ),
              ),
            if (!carUrl.isNotEmpty) Positioned(top: 41, child: emptyWidget()),
            Positioned(child: image('ic_parking.webp', width: 84, height: 30)),
          ],
        ),
      ),
    );
  }

  Widget emptyWidget() {
    return Column(
      children: [
        image('park_item_empty.png', width: 54, height: 14),
        const SizedBox(
          height: 8,
        ),
        Text(T.current!.seat_wait,
            style:
                JRTextStyles.styleFont8W400.copyWith(color: RColors.textGuide)),
      ],
    );
  }
}
